package com.catsic.mod.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.catsic.base.BaseServiceImpl;
import com.catsic.base.exception.CatsicException;
import com.catsic.core.entity.Organ;
import com.catsic.core.entity.Role;
import com.catsic.core.entity.User;
import com.catsic.core.organ.dao.OrganDao;
import com.catsic.core.role.dao.RoleDao;
import com.catsic.core.user.dao.UserDao;
import com.catsic.core.user.service.UserService;
import com.catsic.mod.condition.ReceiveMessageCondition;
import com.catsic.mod.convert.Converts;
import com.catsic.mod.dao.ReceiveMessageDao;
import com.catsic.mod.entity.ReceiveMessage;
import com.catsic.mod.entity.SendMessage;
import com.catsic.mod.exception.CatsicExceptionCode;
import com.catsic.mod.form.ReceiveMessageForm;
import com.catsic.mod.form.SendMessageForm;
import com.catsic.mod.service.ReceiveMessageService;
import com.catsic.mod.service.SendMessageService;
import com.catsic.mod.specification.ReceiveMessageSpec;
import com.catsic.pub.drawer.service.DrawerService;
import com.catsic.pub.form.DrawerForm;

/**
 * 站内信息发件 service实现类
 */
@Service("receiveMessageService")
public class ReceiveMessageServiceImpl extends BaseServiceImpl<ReceiveMessage, String> implements ReceiveMessageService {

	@Autowired
	private ReceiveMessageDao receiveMessageDao;

	@Autowired
	private DrawerService drawerService;
	
	@Autowired
	private OrganDao organDao;
	
	@Autowired
	private RoleDao roleDao;
	
	@Autowired
	private UserDao userDao;
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private SendMessageService sendMessageService;

	
	@Override
	public Page<ReceiveMessageForm> findAll(ReceiveMessageCondition condition,Pageable pageable) {
		String organName = receiveMessageDao.findOrganById(condition.getOrganId());
		User user = receiveMessageDao.findRoleById(condition.getUserId());
		List<String> addresseeName =new ArrayList<String>();
		addresseeName.add(condition.getAddresseeName().get(0));
		addresseeName.add(organName);
		addresseeName.add(user.getRoles().get(0).getName());
		condition.setAddresseeName(addresseeName);
		ReceiveMessageSpec spec = new ReceiveMessageSpec(condition);
		Page<ReceiveMessage> page = receiveMessageDao.findAll(spec, pageable);
		List<ReceiveMessageForm> listReceiveMessageForm = new ArrayList<ReceiveMessageForm>();
		for (ReceiveMessage receiveMessage : page.getContent()) {
			listReceiveMessageForm.add(Converts.convert(receiveMessage));
		}
		Page<ReceiveMessageForm> page_ = new PageImpl<ReceiveMessageForm>(listReceiveMessageForm, pageable, page.getTotalElements());
		return page_;
	}

	@Override
	public List<ReceiveMessageForm> findAddresseeType(String code) {
		List<ReceiveMessageForm> listFrom=new ArrayList<ReceiveMessageForm>();
		List<DrawerForm> drawerList =drawerService.findByParentCode(code);
		for(DrawerForm drawerForm:drawerList){
			ReceiveMessageForm rmf=new ReceiveMessageForm();
			rmf.setAddresseeTypeName(drawerForm.getName());
			rmf.setCode(drawerForm.getCode());
			listFrom.add(rmf);
		}
		return listFrom;
	}

	@Override
	public Page<ReceiveMessageForm> searchAddresseeByType(ReceiveMessageCondition condition,Pageable pageable) {
		List<ReceiveMessageForm> listReceiveMessageForm = new ArrayList<ReceiveMessageForm>();
		Page<ReceiveMessageForm> page_ = null ;
		if(condition.getCode().equals("001000")){
			Page<Organ> page = organDao.findAll(pageable);
			for (Organ organ : page.getContent()) {
				ReceiveMessageForm receiveMessageForm=new ReceiveMessageForm();
				receiveMessageForm.setAddresseeId(organ.getId());
				receiveMessageForm.setAddresseeName(organ.getName());
				receiveMessageForm.setCode(condition.getCode());
				receiveMessageForm.setAddresseeTypeName("机构");
				listReceiveMessageForm.add(receiveMessageForm);
			}
			page_ = new PageImpl<ReceiveMessageForm>(listReceiveMessageForm, pageable, page.getTotalElements());
		}
		if(condition.getCode().equals("001001")){
			Page<Role> page= roleDao.findAll(pageable);
			for (Role role : page.getContent()) {
				ReceiveMessageForm receiveMessageForm=new ReceiveMessageForm();
				receiveMessageForm.setAddresseeId(role.getId());
				receiveMessageForm.setAddresseeName(role.getName());
				receiveMessageForm.setCode(condition.getCode());
				receiveMessageForm.setAddresseeTypeName("角色");
				listReceiveMessageForm.add(receiveMessageForm);
			}
			page_ = new PageImpl<ReceiveMessageForm>(listReceiveMessageForm, pageable, page.getTotalElements());
		}
		if(condition.getCode().equals("001002")){
			Page<User> page= userDao.findAll(pageable);
			for (User user : page.getContent()) {
				ReceiveMessageForm receiveMessageForm=new ReceiveMessageForm();
				receiveMessageForm.setAddresseeId(user.getId());
				receiveMessageForm.setAddresseeName(user.getUsername());
				receiveMessageForm.setCode(condition.getCode());
				receiveMessageForm.setAddresseeTypeName("用户");
				listReceiveMessageForm.add(receiveMessageForm);
			}
			page_ = new PageImpl<ReceiveMessageForm>(listReceiveMessageForm, pageable, page.getTotalElements());
		}
		return page_;
	}

	@Override
	public void add(ReceiveMessageForm receiveMessageForm) {
		User user=userService.findById(receiveMessageForm.getUserId());
		if(receiveMessageForm.getSendMessageId()==null){
			SendMessageForm sendMessageForm=new SendMessageForm();
			sendMessageForm.setTitle(receiveMessageForm.getTitle());
			sendMessageForm.setContent(receiveMessageForm.getContent());
			sendMessageForm.setRemind(receiveMessageForm.getRemind());
			sendMessageForm.setUser(user);
			receiveMessageForm.setSendMessageId(sendMessageService.add(sendMessageForm).getId());
		}
		for (int i=0;i<receiveMessageForm.getAddresseeIds().length;i++) {
			SendMessage sendMessage=sendMessageService.findById(receiveMessageForm.getSendMessageId());
			ReceiveMessage receiveMessage=new ReceiveMessage();
			receiveMessage.setTitle(receiveMessageForm.getTitle());
			receiveMessage.setContent(receiveMessageForm.getUserName()+"："+receiveMessageForm.getContent());
			receiveMessage.setUser(user);
			receiveMessage.setAddresseeId(receiveMessageForm.getAddresseeIds()[i]);
			receiveMessage.setAddresseeName(receiveMessageForm.getAddresseeNames()[i]);
			receiveMessage.setCode(receiveMessageForm.getCodes()[i]);
			receiveMessage.setAddresseeTypeName(receiveMessageForm.getAddresseeTypeNames()[i]);
			receiveMessage.setRemind(receiveMessageForm.getRemind());
			receiveMessage.setSendMessage(sendMessage);
			super.add(receiveMessage);
		}
	}
	
	@Override
	public void backMessage(String receiveMessageId,String UserName,String reContent) {
		ReceiveMessage receiveMessage=findById(receiveMessageId);
		StringBuffer sb=new StringBuffer(receiveMessage.getContent());
		sb.append("                               "+UserName+"回复："+reContent);
		receiveMessage.setContent(sb.toString());
		super.update(receiveMessage);
	}

	@Override
	public ReceiveMessage findById(String id) {
		ReceiveMessage receiveMessage=super.getById(id);
		if(receiveMessage==null){
			throw new CatsicException(CatsicExceptionCode.站内邮件提醒);
		}
		return receiveMessage;
	}

	@Override
	public Integer findCountById(String userId) {
		Integer count=receiveMessageDao.findCountById(userId);
		return count;
	}
}
